{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "vscode": {
     "languageId": "plaintext"
    }
   },
   "source": [
    "# Cross-Framework LLM Tool for CaptainAgent\n",
    "In this tutorial, we demonstrate how to integrate LLM tools from [LangChain Tools](https://python.langchain.com/v0.1/docs/modules/tools), [CrewAI Tools](https://github.com/crewAIInc/crewAI-tools/tree/main) into CaptainAgent. The developers just need to use one line of code to convert them into [AG2 tools](https://docs.ag2.ai/latest/docs/use-cases/notebooks/notebooks/tools_interoperability), and then pass it to CaptainAgent while instantiation, simple as that."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Langchain Tool Integration\n",
    "Langchain readily provides a number of tools at hand. These tools can be integrated into AG2 framework through interoperability.\n",
    "\n",
    "### Installation\n",
    "To integrate LangChain tools into the AG2 framework, install the required dependencies:\n",
    "\n",
    "```bash\n",
    "pip install -U ag2[openai,interop-langchain,duckduckgo]\n",
    "```\n",
    "\n",
    "> **Note:** If you have been using `autogen` or `ag2`, all you need to do is upgrade it using:  \n",
    "> ```bash\n",
    "> pip install -U autogen[openai,interop-langchain,duckduckgo]\n",
    "> ```\n",
    "> or  \n",
    "> ```bash\n",
    "> pip install -U ag2[openai,interop-langchain,duckduckgo]\n",
    "> ```\n",
    "> as `autogen`, and `ag2` are aliases for the same PyPI package.  \n",
    "\n",
    "\n",
    "### Preparation\n",
    "Import necessary modules and tools.\n",
    "- [DuckDuckGoSearchRun](https://python.langchain.com/api_reference/community/tools/langchain_community.tools.ddg_search.tool.DuckDuckGoSearchRun.html) and [DuckDuckGoSearchAPIWrapper](https://python.langchain.com/api_reference/community/utilities/langchain_community.utilities.duckduckgo_search.DuckDuckGoSearchAPIWrapper.html#langchain_community.utilities.duckduckgo_search.DuckDuckGoSearchAPIWrapper): Tools for querying DuckDuckGo.\n",
    "- `Interoperability`: This module acts as a bridge, making it easier to integrate LangChain tools with AG2’s architecture."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.tools import DuckDuckGoSearchRun\n",
    "from langchain_community.utilities import DuckDuckGoSearchAPIWrapper\n",
    "\n",
    "from autogen.interop import Interoperability"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Configure the agents\n",
    "Load the config for LLM, which include API key and model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import autogen\n",
    "\n",
    "config_path = \"OAI_CONFIG_LIST\"\n",
    "llm_config = autogen.LLMConfig.from_json(path=config_path, temperature=0).where(model=\"gpt-5\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Tool Integration\n",
    "We use `Interoperability` to convert the LangChain tool into a format compatible with the AG2 framework."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "interop = Interoperability()\n",
    "\n",
    "api_wrapper = DuckDuckGoSearchAPIWrapper()\n",
    "langchain_tool = DuckDuckGoSearchRun(api_wrapper=api_wrapper)\n",
    "ag2_tool = interop.convert_tool(tool=langchain_tool, type=\"langchain\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then add the tools to CaptainAgent, the main difference from original CaptainAgent initialization is to pass the tool as a list into the `tool_lib` argument. This will let the agents within the nested chat created by CaptainAgent all equipped with the tools. THey can write python code to call the tools and observe the results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from autogen import UserProxyAgent\n",
    "from autogen.agentchat.contrib.captainagent import CaptainAgent\n",
    "\n",
    "# build agents\n",
    "captain_agent = CaptainAgent(\n",
    "    name=\"captain_agent\",\n",
    "    code_execution_config={\"use_docker\": False, \"work_dir\": \"groupchat\"},\n",
    "    agent_lib=\"captainagent_expert_library.json\",\n",
    "    tool_lib=[ag2_tool],  # The main difference lies here: we pass the converted tool to the agent\n",
    "    agent_config_save_path=None,\n",
    "    llm_config=llm_config,\n",
    ")\n",
    "\n",
    "captain_user_proxy = UserProxyAgent(name=\"captain_user_proxy\", human_input_mode=\"NEVER\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "res = captain_user_proxy.initiate_chat(\n",
    "    captain_agent,\n",
    "    message=\"Call a group of experts and search for the word of the day Merriham Webster December 26, 2024\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## CrewAI Tool Integration\n",
    "CrewAI also provides a variety of powerful tools designed for tasks such as web scraping, search, code interpretation, and more. The full list of available tools in CrewAI can be observed [here](https://github.com/crewAIInc/crewAI-tools/tree/main).\n",
    "\n",
    "### Installation\n",
    "Install the required packages for integrating CrewAI tools into the AG2 framework.\n",
    "This ensures all dependencies for both frameworks are installed.\n",
    "\n",
    "```bash\n",
    "pip install -U ag2[openai,interop-crewai]\n",
    "```\n",
    "\n",
    "> **Note:** If you have been using `autogen` or `ag2`, all you need to do is upgrade it using:  \n",
    "> ```bash\n",
    "> pip install -U autogen[openai,interop-crewai]\n",
    "> ```\n",
    "> or  \n",
    "> ```bash\n",
    "> pip install -U ag2[openai,interop-crewai]\n",
    "> ```\n",
    "> as `autogen`, and `ag2` are aliases for the same PyPI package.  \n",
    "\n",
    "### Tool Integration\n",
    "Integrating CrewAI tools into AG2 framework follows a similar pipeline as shown below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from crewai_tools import ScrapeWebsiteTool\n",
    "\n",
    "from autogen.interop import Interoperability\n",
    "\n",
    "interop = Interoperability()\n",
    "crewai_tool = ScrapeWebsiteTool()\n",
    "ag2_tool = interop.convert_tool(tool=crewai_tool, type=\"crewai\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Adding tools to CaptainAgent\n",
    "The process is identical to the above, pass the converted tool to `tool_lib` argument, and all the agents created by CaptainAgent gets access to the tools."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from autogen import UserProxyAgent\n",
    "from autogen.agentchat.contrib.captainagent import CaptainAgent\n",
    "\n",
    "# build agents\n",
    "captain_agent = CaptainAgent(\n",
    "    name=\"captain_agent\",\n",
    "    code_execution_config={\"use_docker\": False, \"work_dir\": \"groupchat\"},\n",
    "    agent_lib=\"captainagent_expert_library.json\",\n",
    "    tool_lib=[ag2_tool],\n",
    "    agent_config_save_path=None,  # If you'd like to save the created agents in nested chat for further use, specify the save directory here\n",
    "    llm_config=llm_config,\n",
    ")\n",
    "\n",
    "captain_user_proxy = UserProxyAgent(name=\"captain_user_proxy\", human_input_mode=\"NEVER\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "message = \"Call experts and Scrape the website https://ag2.ai/, analyze the content and summarize it\"\n",
    "result = captain_user_proxy.initiate_chat(captain_agent, message=message)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(result.summary)"
   ]
  }
 ],
 "metadata": {
  "front_matter": {
   "description": "Cross-Framework LLM Tool for CaptainAgent",
   "tags": [
    "tools",
    "langchain",
    "crewai"
   ]
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
